# Libraries
if (!require(tidyverse)) install.packages('tidyverse', repos='http://cran.us.r-project.org'); library(tidyverse)
Loading required package: tidyverse
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
-- Attaching packages --------------------------------------------------------------------------------------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.6 v purrr 0.3.4
v tibble 3.1.7 v dplyr 1.0.9
v tidyr 1.2.0 v stringr 1.4.0
v readr 2.1.2 v forcats 0.5.1
Warning: package ‘ggplot2’ was built under R version 4.1.3
Warning: package ‘tibble’ was built under R version 4.1.3
Warning: package ‘tidyr’ was built under R version 4.1.3
Warning: package ‘readr’ was built under R version 4.1.3
Warning: package ‘dplyr’ was built under R version 4.1.3
Warning: package ‘forcats’ was built under R version 4.1.2
-- Conflicts ------------------------------------------------------------------------------------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
if (!require(viridis)) install.packages('viridis', repos='http://cran.us.r-project.org'); library(viridis)
Loading required package: viridis
Warning: package ‘viridis’ was built under R version 4.1.2
Loading required package: viridisLite
if (!require(patchwork)) install.packages('patchwork', repos='http://cran.us.r-project.org'); library(patchwork)
Loading required package: patchwork
Warning: package ‘patchwork’ was built under R version 4.1.3
if (!require(hrbrthemes)) install.packages('hrbrthemes', repos='http://cran.us.r-project.org'); library(hrbrthemes)
Loading required package: hrbrthemes
Warning: package ‘hrbrthemes’ was built under R version 4.1.3
Registering Windows fonts with R
NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
if (!require(igraph)) install.packages('igraph', repos='http://cran.us.r-project.org'); library(igraph)
Loading required package: igraph
Warning: package ‘igraph’ was built under R version 4.1.3
Attaching package: ‘igraph’
The following objects are masked from ‘package:dplyr’:
as_data_frame, groups, union
The following objects are masked from ‘package:purrr’:
compose, simplify
The following object is masked from ‘package:tidyr’:
crossing
The following object is masked from ‘package:tibble’:
as_data_frame
The following objects are masked from ‘package:stats’:
decompose, spectrum
The following object is masked from ‘package:base’:
union
if (!require(ggraph)) install.packages('ggraph', repos='http://cran.us.r-project.org'); library(ggraph)
Loading required package: ggraph
Warning: package ‘ggraph’ was built under R version 4.1.3
if (!require(colormap)) install.packages('colormap', repos='http://cran.us.r-project.org'); library(colormap)
Loading required package: colormap
Warning: package ‘colormap’ was built under R version 4.1.3
if (!require(ggplot2)) install.packages('ggplot2', repos='http://cran.us.r-project.org'); library(ggplot2)
if (!require(dplyr)) install.packages('dplyr', repos='http://cran.us.r-project.org'); library(dplyr)
if (!require(plotly)) install.packages('plotly', repos='http://cran.us.r-project.org'); library(plotly)
Loading required package: plotly
Warning: package ‘plotly’ was built under R version 4.1.3
Attaching package: ‘plotly’
The following object is masked from ‘package:igraph’:
groups
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
if (!require(quantmod)) install.packages('quantmod', repos='http://cran.us.r-project.org'); library(quantmod)
Loading required package: quantmod
Warning: package ‘quantmod’ was built under R version 4.1.3
Loading required package: xts
Warning: package ‘xts’ was built under R version 4.1.3
Loading required package: zoo
Warning: package ‘zoo’ was built under R version 4.1.3
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Attaching package: ‘xts’
The following objects are masked from ‘package:dplyr’:
first, last
Loading required package: TTR
Warning: package ‘TTR’ was built under R version 4.1.3
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
if (!require(tidygraph)) install.packages('tidygraph', repos='http://cran.us.r-project.org'); library(tidygraph)
Loading required package: tidygraph
Warning: package ‘tidygraph’ was built under R version 4.1.3
Attaching package: ‘tidygraph’
The following object is masked from ‘package:igraph’:
groups
The following object is masked from ‘package:stats’:
filter
1.- LINE CHART
# Crear data frame
vida <- data.frame(any=c(1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000),
esp_home=c(46.3, 48.4, 53.6 ,58.1, 60.8, 65.6, 66.6, 67.1, 70, 71.8, 74),
esp_dona=c(48.3, 51.8, 54.6, 61.6, 65.2, 71.1, 73.1, 74.7, 77.4, 78.8, 80.1))
# Plot
p0 = ggplot() +
geom_line(data=vida, aes(x = any, y = esp_home, color = "blue")) +
geom_line(data=vida, aes(x = any, y = esp_dona, color = "red")) +
scale_color_manual(name = "Sexe", labels=c("Home", "Dona"), values = c("blue", "red")) +
theme_ipsum() +
xlab('Any') +
ylab('Esperança de vida en anys') +
ggtitle("Esperança de vida USA")
p0
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
2.- ARC DIAGRAM
# A really simple edge list
airports <- data.frame(name=c("DFW", "ORD", "LAX", "DEN", "ATL"),
label=c("Dallas", "Chicago", "Los Angeles", "Denver", "Atlanta"))
# links=data.frame(
# source=c("DFW", "DFW", "DFW", "DFW", "ORD", "ORD", "ORD", "ORD", "LAX", "LAX", "LAX", "LAX", "DEN", "DEN", "DEN", "DEN", "ATL", "ATL", "ATL", "ATL"),
# target=c("ORD", "LAX", "DEN", "ATL", "DFW", "LAX", "DEN", "ATL", "DFW", "ORD", "DEN", "ATL", "DFW", "ORD", "LAX", "ATL", "DFW", "ORD", "LAX", "DEN"),
# routes=c(20, 19, 20, 17, 12, 15, 20, 22, 10, 16, 19, 22, 15, 18, 17, 14, 14, 20, 11, 11)
# )
links=data.frame(
source=c("DFW", "DFW", "DFW", "DFW", "ORD", "LAX", "DEN","ATL"),
target=c("ORD", "LAX", "DEN", "ATL", "DFW", "DFW", "DFW", "DFW"),
routes=c(20, 19, 20, 17, -20, -19, -18, -20),
color=c("blue", "blue", "blue", "blue", "red", "red", "red", "red")
)
# Make a cord diagram
net.tidy <- tbl_graph(nodes = airports, edges = links, directed = TRUE)
p3 <- ggraph(net.tidy, layout = "linear") +
geom_edge_arc(arrow = arrow(type = "closed", length = unit(3, 'mm')), aes(colour = color, width = abs(routes), label=abs(routes)),
alpha = 0.8, start_cap = circle(.1, unit = "native"), end_cap = circle(.1, unit = "native")) +
scale_edge_width(range = c(0.2, 2)) +
geom_node_text(aes(label = label), repel = FALSE) +
labs(edge_width = "Numero de rutas") +
theme_graph()+
theme(legend.position = "none") +
labs(caption = paste0('Vols amb origen i destí Dallas '))
# Make a cord diagram
p3
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
3.- OHLC CHART
getSymbols("GOOG",src='yahoo')
[1] "GOOG"
df <- data.frame(Date=index(GOOG),coredata(GOOG))
df <- tail(df, 30)
fig <- df %>% plot_ly(x = ~Date, type="ohlc",
open = ~GOOG.Open, close = ~GOOG.Close,
high = ~GOOG.High, low = ~GOOG.Low)
fig <- fig %>% layout(title = "Cotització Google",
xaxis = list(title="Data", rangeslider = list(visible = F)),
yaxis = list(title="Cotització ($)", rangeslider = list(visible = F)))
fig